var getCookie = function(name) {
	var arr;
	var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg)) {
		return arr[2];
	} else
		return null;
};

$(document).ready(function() {
	var id = getCookie("id");
	$.ajax({
		url : "findName?id=" + id,
		type : "post",
		async:false,
		dataType : "json",
		postData : {
			'id' : id,
		},
		success : function(data) {
			$("#i1").val(data.Customerlist[0].id);
			$("#i2").val(data.Customerlist[0].customerName);	

		},
		error: function(){
			alert("请求错误")
		}
	});

	$("#updatebtn").click(function() {
		update();
	});
});

$(document).ready(function() {
	var id = getCookie("id");
	$.ajax({
				url : "findName?id=" + id,
				type : "post",
				async:false,
				dataType : "json",
				postData : {
					'id' : id,
				},
				success : function(data) {
					$("#input2").val(data.Customerlist[0].customerName);
					if (data.Customerlist[0].gender == "男") {
						$("#male").attr("checked", "true")
					}
					if (data.Customerlist[0].gender == "女") {
						$("#female").attr("checked", "true")
					}
					$("#input3").val(data.Customerlist[0].tel);
					$("#input4").val(data.Customerlist[0].UID);
					$("#input5").val(data.Customerlist[0].address);
					$("#input6").val(data.Customerlist[0].customerWork);
					for (var i = 0; i < 4; i++) {
						if ($("#select3").find("option").eq(i).html() == data.Customerlist[0].customerType) {
							$("#select3").find("option").eq(i).attr(
									"selected", "true")
						}	
					}
					if (data.Customerlist[0].discredit == "是") {
						$("#discride1").attr("checked", "true")
					}
					if (data.Customerlist[0].discredit == "否") {
						$("#discride2").attr("checked", "true")
					}
					if (data.Customerlist[0].litigation == "是") {
						$("#lit1").attr("checked", "true")
					}
					if (data.Customerlist[0].litigation == "否") {
						$("#lit2").attr("checked", "true")
					}
					$("#input8").val(data.Customerlist[0].monthlyIncome);
					for (var i = 0; i < 6; i++) {
						if ($("#select1").find("option").eq(i).html() == data.Customerlist[0].bankName) {
							$("#select1").find("option").eq(i).attr(
									"selected", "true")
						}	
					}
					for (var i = 0; i < 4; i++) {
						if ($("#select2").find("option").eq(i).html() == data.Customerlist[0].pledgeName) {
							$("#select2").find("option").eq(i).attr(
									"selected", "true")
						}	
					}
					for (var i = 0; i < 4; i++) {
						if ($("#select4").find("option").eq(i).html() == data.Customerlist[0].customerState) {
							$("#select4").find("option").eq(i).attr(
									"selected", "true")
						}	
					}
				}
			});
});

function update() {
	var id = getCookie("id");
	console.log("id:"+id+",customerName:"+$("#input2").val()+$("#select3 option:selected").text()                 );
	$.ajax({
				url : "update",
				type : "post",
				async:false,
				dataType : "json",
				data : {
					"id":id,
					"customername" : $("#input2").val(),
					"gender" : $(':radio[name="sex"]:checked').val(),
					"tel" : $("#input3").val(),
					"uid" : $("#input4").val(),
					"address" : $("#input5").val(),
					"customerwork" : $("#input6").val(),
					"customertype" : $("#select3 option:selected").text(),
					"discredit" : $(':radio[name="yes"]:checked').val(),
					"litigation" : $(':radio[name="no"]:checked').val(),
					"monthlyincome" : $("#input8").val(),
					"bankid" : $("#select1 option:selected").val(),
					"goodsid" : $("#select2 option:selected").val(),
					"customerstate" : $("#select4 option:selected").text(),
					"createtime":null,
				},
				success : function(data) {
					alert("修改成功");
				}
			});

};
